package com.beidouin.sensortest;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import com.friendlyarm.AndroidSDK.GPIOEnum;
import com.friendlyarm.AndroidSDK.HardwareControler;

public class BodyActivity extends AppCompatActivity implements Runnable {


    TextView bodytxv;
    Handler handler;
    Thread t = new Thread(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_body);

        bodytxv= (TextView) findViewById(R.id.body_key);
        HardwareControler.exportGPIOPin(73);//使能端口
        HardwareControler.setGPIODirection(73, GPIOEnum.IN);//设置GPIO为输入模式


        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                if (msg.what == 0x101) {
                    bodytxv.setText(msg.getData().getBoolean("button")?"未发现人体":"发现人体");
                }
                super.handleMessage(msg);
            }
        };
        t.start();
    }

    @Override
    protected void onDestroy() {
        //将线程销毁掉
        t.interrupt();
        super.onDestroy();
    }

    @Override
    public void run() {
        while (!Thread.currentThread().isInterrupted())
        {
            Message m=handler.obtainMessage();
            boolean bodykey;
            if (HardwareControler.getGPIOValue(73) == GPIOEnum.LOW) {
                bodykey=true;
            } else {
                bodykey=false;
            }
            m.what=0x101;
            Bundle bundle =new Bundle();
            bundle.putBoolean("button",bodykey);
            m.setData(bundle);
            handler.sendMessage(m);
            if(bodykey==true)
            {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
